package com.intel.two.service.building.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.bo.building.ResidentBo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.mapper.buildingMapper.ResidentMapper;
import com.intel.two.pojo.building.Resident;
import com.intel.two.service.building.ResidentService;
import com.intel.two.vo.building.BarVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("residentService")
public class ResidentServiceImpl implements ResidentService {
    @Autowired
    private ResidentMapper residentMapper;

    @Override
    public LayuiPageVo<Resident> residentPageData(Map<String, Object> params) {
        //mybatis 分页 插件的使用
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);
        List<Resident> residents = residentMapper.residentPageData(params);
        System.out.println("居民列表："+residents);

        // mybatis 的分页数据结构
        PageInfo<Resident> residentPageInfo = new PageInfo<>(residents);
        LayuiPageVo<Resident> residentLayuiPageVo = new LayuiPageVo<>();

        residentLayuiPageVo.setCode(0);
        residentLayuiPageVo.setMsg("居民列表");
        residentLayuiPageVo.setCount(residentPageInfo.getTotal());
        residentLayuiPageVo.setData(residentPageInfo.getList());

        return residentLayuiPageVo;
    }

    @Override
    public int addResident(Resident resident) {
        return residentMapper.addResident(resident);
    }

    @Override
    public int deleteResident(Integer id) {
        return residentMapper.deleteResident(id);
    }

    @Override
    public int updateResident(Resident resident) {
        return residentMapper.updateResident(resident);
    }

    @Override
    public List<BarVo> BarData() {
        return residentMapper.BarData();
    }

    @Override
    public List<ResidentBo> ResidentExcelData(Resident resident) {
        List<Resident> employeeExcelData = residentMapper.ResidentExcelData(resident);
        List<ResidentBo> collect = employeeExcelData.stream()
                .map((residents) -> {
                    ResidentBo residentBo = new ResidentBo();
                    BeanUtils.copyProperties(residents, residentBo);
                    return residentBo;
                }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public int deleteResidents(Map<String, Object> map) {
        String str = (String) map.get("id");
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
            System.out.println(strs[i]);
        }
        return residentMapper.deleteResidents(list);
    }
}
